{% extends "root/_incs/lay_rand.htm" %}

{% block title %}<title>随机数应用 - 图表</title>{% endblock %}

{% block main %}
<script src="https://www.chartjs.org/dist/2.8.0/Chart.min.js"></script>
<script src="https://www.chartjs.org/samples/latest/utils.js"></script>
<style>
canvas{
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}
</style>

<div class="out">

<form id="sofm" name="sofm" method="get" action="?">
<table width="100%" border="1" class="tbform">
  <tr>
    <td>
        最小<input name="min" data-maxlength="6" value="{{d.data.argv.mi}}">
    </td>
    <td>
        涨幅<input name="ins" data-maxlength="24" value="{{d.data.argv.ins}}">
    </td>
  </tr> 
  <tr>
    <td>
        最大<input name="max" data-maxlength="24" value="{{d.data.argv.ma}}">
    </td>
    <td>
        小数<input name="dot" data-maxlength="24" value="{{d.data.argv.do}}">
    </td>
  </tr> 
  <tr>
    <td>
        共<input name="all" data-maxlength="6" value="{{d.data.argv.ax}}">个
    </td>
    <td><input type="submit" value="生成" class="w60"></td>
  </tr> 
</table>
</form>

    <div style="width:100%;margin:auto;">
        <canvas id="canvas"></canvas>
    </div>

<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
<button id="addData">Add Data</button>
<button id="removeData">Remove Data</button>

</div><!--end.out-->

<script>
    var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var config = {
        type: 'line',
        data: {
            labels: ['2018-06', '2018-07', '2018-08', '2018-09', '2018-10', '2018-11', '2018-12', '2019-13', '2019-02', '2019-03', '2019-04', '2019-05'],
            datasets: [{
                label: '月活跃数(万)',
                backgroundColor: window.chartColors.blue,
                borderColor: window.chartColors.blue,
                data: [
                    {{ d.data.tabs }}
                ],
                fill: false,
            }/*, {
                label: 'My Second dataset',
                fill: false,
                backgroundColor: window.chartColors.red,
                borderColor: window.chartColors.red,
                data: [
                    randomScalingFactor(), randomScalingFactor()
                ],
            }*/]
        },
        options: {
            elements: {
                line: {
                    tension: 0 // disables bezier curves
                }
            },
            legend: {
                labels: {
                    fontColor: 'black'
                }
            },
            responsive: true,
            title: {
                display: true,
                text: '2018-06 ~ 2019-05 XXX-会员统计数'
            },
            tooltips: {
                mode: 'index',
                intersect: false,
            },
            hover: {
                mode: 'nearest',
                intersect: true
            },
            scales: {
                xAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: '统计时间(1年)'
                    }
                }],
                yAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: '(万)会员统计数'
                    }
                }]
            }
        }
    };

    window.onload = function() {
        var ctx = document.getElementById('canvas').getContext('2d');
        window.myLine = new Chart(ctx, config);
    };

    document.getElementById('randomizeData').addEventListener('click', function() {
        config.data.datasets.forEach(function(dataset) {
            dataset.data = dataset.data.map(function() {
                return randomScalingFactor();
            });

        });

        window.myLine.update();
    });

    var colorNames = Object.keys(window.chartColors);
    document.getElementById('addDataset').addEventListener('click', function() {
        var colorName = colorNames[config.data.datasets.length % colorNames.length];
        var newColor = window.chartColors[colorName];
        var newDataset = {
            label: 'Dataset ' + config.data.datasets.length,
            backgroundColor: newColor,
            borderColor: newColor,
            data: [],
            fill: false
        };

        for (var index = 0; index < config.data.labels.length; ++index) {
            newDataset.data.push(randomScalingFactor());
        }

        config.data.datasets.push(newDataset);
        window.myLine.update();
    });

    document.getElementById('addData').addEventListener('click', function() {
        if (config.data.datasets.length > 0) {
            var month = MONTHS[config.data.labels.length % MONTHS.length];
            config.data.labels.push(month);

            config.data.datasets.forEach(function(dataset) {
                dataset.data.push(randomScalingFactor());
            });

            window.myLine.update();
        }
    });

    document.getElementById('removeDataset').addEventListener('click', function() {
        config.data.datasets.splice(0, 1);
        window.myLine.update();
    });

    document.getElementById('removeData').addEventListener('click', function() {
        config.data.labels.splice(-1, 1); // remove the label first

        config.data.datasets.forEach(function(dataset) {
            dataset.data.pop();
        });

        window.myLine.update();
    });
</script>

{% endblock %}

